package com.yoyo.itravel.service.impl;

import com.yoyo.itravel.bean.SightTicketStock;
import com.yoyo.itravel.dao.ISightTicketStockDao;
import com.yoyo.itravel.exceptions.SightStockException;
import com.yoyo.itravel.service.ISightTicketStockService;
import com.yoyo.itravel.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;

/**
 * @Description 景点门票库存操作服务层接口实现类
 * 
 * @Author HZX
 * @Date 2020/6/23 21:36
 * @Version 1.0
 */
@Service
public class SightTicketStockServiceImpl implements ISightTicketStockService {

    @Autowired
    private ISightTicketStockDao sightStockDao;

    /**
     * @Description 插入景点门票库存信息
     * @Param sightStock
     * @Return boolean
     */
    @Override
    public boolean addSightStock(SightTicketStock sightStock) throws SightStockException {
        // 判断sightStock对象是否为null
        if (sightStock == null) {
            throw new SightStockException("传入的SightStock对象为null...");
        }
        try {
            // 判断存入的日期和景点门票具体类型是否在数据库中已存在，已存在则不能添加
            if (sightStockDao.selectByCondition(new SightTicketStock(sightStock.getDate(), sightStock.getDetailTypeId())) != null) {
                // 已存在，抛出异常
                throw new SightStockException("存入的日期和景点门票具体类型在数据库中已存在！");
            }
            return sightStockDao.insert(sightStock) == 1;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new SightStockException("插入景点门票库存信息失败！");
        }
    }

    /**
     * @Description 通过景点门票库存id修改景点门票库存信息
     * @Param sightTicketStock
     * @Return boolean
     */
    @Override
    public boolean updateById(SightTicketStock sightTicketStock) throws SightStockException {
        // 判断sightStock对象是否为null
        if (sightTicketStock == null) {
            throw new SightStockException("传入的SightStock对象为null...");
        }
        try {
            return sightStockDao.updateById(sightTicketStock) == 1;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new SightStockException("通过景点门票库存id修改景点门票库存信息失败！");
        }
    }

    /**
     * @Description 通过景点门票库存id删除景点门票库存信息
     * @Param ticketStockId
     * @Return boolean
     */
    @Override
    public boolean deleteById(String ticketStockId) throws SightStockException {
        // 判断ticketStockId是否为空
        if (StringUtils.isEmpty(ticketStockId)) {
            throw new SightStockException("传入的ticketStockId为空...");
        }
        try {
            return sightStockDao.deleteById(ticketStockId) == 1;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new SightStockException("通过景点门票库存id修改景点库存信息失败！");
        }
    }

    /**
     * @Description 根据条件得到对应的景点门票库存
     * @Param sightStock
     * @Return Object
     */
    @Override
    public Object findByCondition(SightTicketStock sightStock) throws SightStockException {
        // 判断sightStock对象是否为null
        if (sightStock == null) {
            throw new SightStockException("传入的SightStock对象为null...");
        }
        try {
            return sightStockDao.selectByCondition(sightStock);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new SightStockException("根据条件得到对应的景点门票库存失败！");
        }
    }

    /**
     * @Description 根据条件得到对应的景点门票库存
     *
     * @Param ticketStockId
     * @Return com.yoyo.itravel.bean.SightStock
     */
    @Override
    public SightTicketStock findById(String ticketStockId) throws SightStockException {
        // 判断ticketStockId是否为空
        if (StringUtils.isEmpty(ticketStockId)) {
            throw new SightStockException("传入的ticketStockId为空...");
        }
        try {
            return sightStockDao.selectById(ticketStockId);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new SightStockException("根据条件得到对应的景点门票库存失败！");
        }
    }

    /**
     * @Description 获得所有的景点门票库存
     *
     * @Param
     * @Return java.util.List<com.yoyo.itravel.bean.SightStock>
     */
    @Override
    public List<SightTicketStock> findAll() throws SightStockException {
        try {
            return sightStockDao.selectAll();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new SightStockException("获得所有的景点门票库存信息失败！");
        }
    }
}
